<?php
namespace User\Controller;

use Common\Controller\MemberbaseController;
use Common\Log\TypeLog;

class FavoriteController extends MemberbaseController{
	
    // 前台个人中心我的收藏列表
	public function index(){
		$uid=sp_get_current_userid();
		$user_favorites_model=M("UserFavorites");
		$where=array("uid"=>$uid);
		
		$count=$user_favorites_model->where($where)->count();
		
		$page=$this->page($count,10);
		
		$favorites=$user_favorites_model->where($where)
		->alias("a")
		->join("__POSTS__ c ON a.object_id = c.id")
		->order("createtime desc")
		->limit($page->firstRow,$page->listRows)
		->select();
		
		$this->assign("page",$page->show("default"));
		$this->assign($this->user);
		$this->assign("favorites",$favorites);
		$this->display(":favorite");
	}
	
	// 用户收藏
	public function do_favorite(){
        $id         = I("get.id", 0, 'intval');
        $from_id    = I("get.from_id", 0, 'intval');
        $find_favorite = TypeLog::AddCollectLog(sp_get_current_userid(), $from_id, $id);
        if($find_favorite['status'] == -1){
            $this->error("亲，您已收藏过啦！");
        } elseif ($find_favorite['status'] == 1) {
            M("Posts")->save(array("id" => $id, "collect" => array("exp", "collect + 1")));
            $this->success("收藏成功！");
        } else {
            $this->error("收藏失败！");
        }
	}
	
	// 用户取消收藏
	public function delete_favorite(){
		$id=I("get.id",0,"intval");
		$uid=sp_get_current_userid();
		$post['uid']=$uid;
		$user_favorites_model=M("UserFavorites");
		$result=$user_favorites_model->where(array('id'=>$id,'uid'=>$uid))->delete();
		if($result){
			$this->success("取消收藏成功！");
		}else {
			$this->error("取消收藏失败！");
		}
	}
}